//
//  ZDBVideoPlayerViewController.m
//  ZDB
//
//  Created by 微笑吧阳光 on 2020/3/31.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBVideoPlayerViewController.h"
#import <ZFPlayer/ZFPlayer.h>
#import <ZFPlayer/ZFAVPlayerManager.h>
#import "MZTimerLabel.h"

@interface ZDBVideoPlayerViewController ()<MZTimerLabelDelegate>
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UILabel * guanggaolab2;
@property (nonatomic, strong)MZTimerLabel *timer2;
@property (nonatomic, strong)UIButton *doneBtn;
@end

@implementation ZDBVideoPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"#333333"];
    /// 播放器相关
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    self.player = [[ZFPlayerController alloc] initWithPlayerManager:playerManager containerView:self.view];
    //self.player.controlView = self.controlView;
    self.player.orientationObserver.supportInterfaceOrientation = ZFInterfaceOrientationMaskPortrait;
    [self.player enterFullScreen:NO animated:NO];
    playerManager.assetURL = [NSURL URLWithString:[self.infoDicc jsonString:@"url"]];
    self.player.shouldAutoPlay = YES;
    
    
    UIButton * closeBtn = [BBControl createButtonWithFrame:CGRectMake(15, kStatusBarAndNavigationBarHeight-30, 30, 30) target:self SEL:@selector(closeBtnClik) title:@""];
    [closeBtn setImage:[UIImage imageNamed:@"Exchange_close"] forState:0];
    [self.view addSubview:closeBtn];
    
//    self.doneBtn = [BBControl createButtonWithFrame:CGRectMake(kScreenWidth-65-10, closeBtn.top, 65, 30) target:self SEL:@selector(doneBtnBtnClik) title:@"领取奖励"];
//    [self.doneBtn setTitleColor:MAINCOLOR forState:0];
//    self.doneBtn.hidden = YES;
//    [self.doneBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
//    [self.view addSubview:self.doneBtn];
    
    UIView * backView = [BBControl createViewWithFrame:CGRectMake(0, kStatusBarAndNavigationBarHeight+5, kScreenWidth, 45)];
    backView.backgroundColor = kRGBAColor(38, 39, 40, 0.7);
    [self.view addSubview:backView];
    
    UILabel * guanggaolab = [BBControl createLabelWithFrame:CGRectMake(15, 12.5, 30, 20) Font:12 Text:@"广告"];
    guanggaolab.font = [UIFont systemFontOfSize:12];
    guanggaolab.textColor = [UIColor whiteColor];
    [backView addSubview:guanggaolab];
    UIImageView *iconimage = [BBControl createImageViewFrame:CGRectMake(45, 15, 15, 15) imageName:@"xiajiantou-2.png"];
    [backView addSubview:iconimage];
     self.guanggaolab2= [BBControl createLabelWithFrame:CGRectMake(kScreenWidth-15-75, 2.5, 75, 40) Font:12 Text:@"后可获得奖励"];
    self.guanggaolab2.textAlignment = NSTextAlignmentRight;
    self.guanggaolab2.textColor = [UIColor whiteColor];
    self.guanggaolab2.font = [UIFont systemFontOfSize:12];
    [backView addSubview:self.guanggaolab2];
    
    
    
    self.timer2 = [[MZTimerLabel alloc] initWithFrame:CGRectMake(self.guanggaolab2.frame.origin.x-40, 2.5, 40, 40)];
      self.timer2.timerType = MZTimerLabelTypeTimer;
    [backView addSubview:self.timer2];
    self.timer2.resetTimerAfterFinish = NO;
    self.timer2.delegate = self;
      //do some styling
    self.timer2.timeLabel.backgroundColor = [UIColor clearColor];
    self.timer2.timeLabel.font = [UIFont systemFontOfSize:12.0f];
    self.timer2.timeLabel.textColor = [UIColor whiteColor];
    self.timer2.timeLabel.textAlignment = NSTextAlignmentCenter; //UITextAlignmentCenter
    [self.timer2 setCountDownTime:[self.infoDicc jsonInteger:@"seeTime"]];
    self.timer2.timeFormat = @"ss秒";
    [self.timer2 start];
    
    
    
    
    
}
- (void)timerLabel:(MZTimerLabel*)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime{
    NSLog(@"finshedCountDownTimerWithTime");
    self.timer2.hidden = YES;
    self.guanggaolab2.text = @"完成浏览";
    [self userSeeAdvertisementComplete];
    //self.doneBtn.hidden = NO;
}
-(void)closeBtnClik{
     self.timer2.hidden = YES;
    [self.timer2 pause];
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}
-(void)doneBtnBtnClik{
    
    
    
    
}

-(void)userSeeAdvertisementComplete{
    
    WEAKSELF
    NSString  * signInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"userSeeAdvertisementComplete"];
    NSMutableDictionary * dataDicc = [NSMutableDictionary dictionary];
    [dataDicc setValue:@([self.infoDicc jsonInteger:@"id"]) forKey:@"id"];
    [[SmileHttpTool sharedInstance] POST:signInfo parameters:dataDicc origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        STRONGSELFFor(weakSelf);
        if (statusCode==0) {
              [JMNotifyView showNotify:[NSString stringWithFormat:@"已领取%ld积分",[strongSelf.infoDicc jsonInteger:@"integral"]] isSuccessful:YES];
        }else{
            [JMNotifyView showNotify:message isSuccessful:NO];
            [strongSelf showTryActionAlert];
        }
    } failure:^(id responseObject, NSError *error) {
      STRONGSELFFor(weakSelf);
      [strongSelf showTryActionAlert];
    }];

}

-(void)showTryActionAlert{
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"获取积分请求失败，是否重新领取？" preferredStyle:UIAlertControllerStyleAlert];//UIAlertControllerStyleAlert视图在中央
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    WEAKSELF
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"再试一次" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        STRONGSELFFor(weakSelf);
        [strongSelf userSeeAdvertisementComplete];
        
     }];//https在iTunes中找，这里的事件是前往手机端App store下载微信
     [alertController addAction:cancelAction];
     [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
    
    
}
//
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    self.player.viewControllerDisappear = NO;
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    self.player.viewControllerDisappear = YES;
//}
//
//- (UIStatusBarStyle)preferredStatusBarStyle {
//    if (self.player.isFullScreen) {
//        return UIStatusBarStyleLightContent;
//    }
//    return UIStatusBarStyleDefault;
//}
//
//- (BOOL)prefersStatusBarHidden {
//    return self.player.isStatusBarHidden;
//}
//
//- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
//    return UIStatusBarAnimationSlide;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
